package com.ss.android.article.base.ui;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.bytedance.common.utility.UIUtils;

import java.util.ArrayList;

public class MultiStyleTextView extends View {
    private Resources a;
    private DisplayMetrics b;
    private int c;
    private int d;
    private int e;
    private String f;
    private String g;
    private Paint h;
    private Paint i;
    private Paint j;
    private Paint.FontMetrics k;
    private Paint.FontMetrics l;
    private float m;
    private float n;
    private int o;
    private ArrayList p;
    private int q;
    private boolean r;
    private boolean s;
    private boolean t;
    private boolean u;
    private boolean v;
    private Rect w;
    private Rect x;
    private int y;

    public MultiStyleTextView(Context arg3) {
        super(arg3);
        this.g = "全文";
        this.q = -1;
        this.r = false;
        this.s = false;
        this.t = false;
        this.u = false;
        this.v = false;
        this.y = 2147483647;
        this.a();
    }

    public MultiStyleTextView(Context arg3, AttributeSet arg4) {
        super(arg3, arg4);
        this.g = "全文";
        this.q = -1;
        this.r = false;
        this.s = false;
        this.t = false;
        this.u = false;
        this.v = false;
        this.y = 2147483647;
        this.a();
    }

    public MultiStyleTextView(Context arg3, AttributeSet arg4, int arg5) {
        super(arg3, arg4, arg5);
        this.g = "全文";
        this.q = -1;
        this.r = false;
        this.s = false;
        this.t = false;
        this.u = false;
        this.v = false;
        this.y = 2147483647;
        this.a();
    }

    private void a() {
        this.setClickable(true);
        this.a = this.getResources();
        this.b = this.a.getDisplayMetrics();
        DisplayMetrics v0 = this.a.getDisplayMetrics();
        this.e = ((int) TypedValue.applyDimension(1, 4f, v0));
        int v1 = this.a.getColor(com.ss.android.article.browser.R.color.ssxinzi1);
        int v2 = this.a.getColor(com.ss.android.article.browser.R.color.update_comment_more_text_selector);
        float v3 = TypedValue.applyDimension(2, 17f, v0);
        float v0_1 = TypedValue.applyDimension(2, 17f, v0);
        this.h = new Paint();
        this.h.setAntiAlias(true);
        this.h.setTextAlign(Paint.Align.LEFT);
        this.h.setColor(v1);
        this.h.setTextSize(v3);
        this.i = new Paint();
        this.i.setAntiAlias(true);
        this.i.setTextAlign(Paint.Align.LEFT);
        this.i.setColor(v2);
        this.i.setTextSize(v0_1);
        this.k = this.h.getFontMetrics();
        this.l = this.i.getFontMetrics();
        this.m = this.k.descent - this.k.ascent;
        this.n = this.l.descent - this.l.ascent;
        this.p = new ArrayList();
    }

    private static Rect a(int arg5, int arg6, int arg7, int arg8, int arg9, int arg10) {
        return new Rect(arg5 - arg9, arg6 - arg10, arg5 + arg7 + arg9, arg6 + arg8 + arg10);
    }

    private void a(Canvas arg3, float arg4, float arg5) {
        if (this.s) {
            arg3.drawText(this.g, arg4, arg5, this.i);
        }
    }

    private void a(Canvas arg8, float arg9, float arg10, float arg11, float arg12) {
        int v6 = 15;
        int v4 = 5;
        if (this.j != null) {
            if (this.w == null || (this.r)) {
                int v1 = ((int) (this.l.ascent + arg10));
                this.w = MultiStyleTextView.a(((int) arg9), v1, ((int) arg11), ((int) arg12), v4, v4);
                this.x = MultiStyleTextView.a(((int) arg9), v1, ((int) arg11), ((int) arg12), v6, v6);
            }

            if (!this.v) {
                return;
            }

            arg8.drawRect(this.w, this.j);
        }
    }

    public final void a(String arg3, String arg4) {
        this.f = arg3;
        this.g = arg4;
        this.r = true;
        this.s = false;
        if (this.y <= 0) {
            this.setMaxLines(this.y);
        }

        if (TextUtils.isEmpty(((CharSequence) arg4))) {
            this.c = 0;
        }

        this.invalidate();
        this.requestLayout();
    }

    private void b() {
        String v0 = this.f == null ? "" : this.f;
        this.f = v0;
        v0 = this.g == null ? "" : this.g;
        this.g = v0;
    }

    private int getLineCount() {
        int v1;
        MultiStyleTextView v0_1;
        float[] v6 = null;
        float v11 = 0.999f;
        int v4 = 1;
        int v2 = 0;
        if (!TextUtils.isEmpty(this.f)) {
            if (this.q > 0 && !this.r) {
                return this.q;
            }

            this.r = false;
            this.b();
            int v8 = this.getMeasuredWidth();
            if (v8 == 0) {
                return v2;
            }

            int v0 = ((int) (this.h.measureText(this.f) + v11));
            int v9 = ((int) (this.i.measureText(this.g) + v11));
            if (v0 + v9 + this.c <= v8) {
                this.t = false;
            } else {
                if (v0 >= v8 || v0 + v9 + this.c <= v8) {
                    int v10 = this.o;
                    String v1_1 = this.f;
                    this.p.clear();
                    int v7 = 0;
                    while (true) {
                        int v3 = this.h.breakText(((CharSequence) v1_1), 0, v1_1.length(), true, ((float) v10), v6);
                        v0 = v7 + 1;
                        if (v3 == v1_1.length()) {
                            this.p.add(v1_1);
                            break;
                        } else if (v0 == this.y) {
                            StringBuilder v3_1 = new StringBuilder("...");
                            String v0_2 = TextUtils.isEmpty(this.g) ? "全文" : this.g;
                            this.p.add(v1_1.substring(0, this.h.breakText(((CharSequence) v1_1), 0, v1_1.length(), true, ((float) (v10 - (((int) this.i.measureText(v3_1.append(v0_2).toString()))))), v6)) + "...");
                            this.s = true;
                            break;
                        } else {
                            if (v3 > v1_1.length()) {
                                v3 = v1_1.length();
                            }

                            this.p.add(v1_1.substring(0, v3));
                            v1_1 = v1_1.substring(v3);
                            v7 = v0;
                            continue;
                        }
                    }

                    float v1_2 = ((float) (((int) (this.h.measureText((String) this.p.get(this.p.size() - 1)) + v11))));
                    v0 = this.p.size();
                    if (v1_2 + (((float) v9)) + (((float) this.c)) > (((float) v8))) {
                        v1 = v0;
                        v0_1 = this;
//                        goto label_43;
                    } else {
                        v4 = 0;
                        v1 = v0;
                        v0_1 = this;
                    }
                } else {
                    v0_1 = this;
                    v1 = 1;
                }

//                label_43:
                v0_1.t = v4 > 0;
                v4 = v1;
            }

            this.q = v4;
            v2 = this.q;
        }

        return v2;
    }

    public int getScreenWidth() {
        int v0 = UIUtils.getScreenWidth(this.getContext());
        int v1 = UIUtils.getScreenHeight(this.getContext());
        if (v0 >= v1) {
            v0 = v1;
        }

        return v0;
    }

    public String getText() {
        String v0 = android.support.design.a.isTextEmpty(this.f) ? "" : this.f;
        return v0;
    }

    protected void onDraw(Canvas arg15) {
        float v3_1;
        float v13 = 2f;
        float v12 = 0.999f;
        super.onDraw(arg15);
        if (!TextUtils.isEmpty(this.f)) {
            this.b();
            int v9 = this.getMeasuredWidth();
            int v1 = this.getMeasuredHeight();
            int v3 = ((int) (this.h.measureText(this.f) + v12));
            int v10 = ((int) (this.i.measureText(this.g) + v12));
            int v4 = this.getLineCount();
            if (v4 > 0) {
                if (v4 != 1) {
                    if (this.t) {
                        int v7 = this.p.size();
                        int v6;
                        for (v6 = 0; true; ++v6) {
                            if (v6 >= v7) {
                                return;
                            }

                            arg15.drawText((String) this.p.get(v6), 0f, (((float) this.d)) - this.k.ascent + (((float) v6)) * (this.m + (((float) this.e))), this.h);
                            if (v6 == v7 - 1) {
                                v3_1 = (((float) this.d)) + (((float) (v6 + 1))) * (this.m + (((float) this.e))) - this.l.ascent;
                                this.a(arg15, 0f, v3_1, ((float) v10), this.n);
                                this.a(arg15, 0f, v3_1);
                            }
                        }
                    }

                    int v11 = this.p.size();
                    for (v1 = 0; v1 < v11; ++v1) {
                        Object v0 = this.p.get(v1);
                        arg15.drawText(((String) v0), 0f, (((float) this.d)) - this.k.ascent + (((float) v1)) * (this.m + (((float) this.e))), this.h);
                        if (v1 == v11 - 1) {
                            float v0_1 = ((float) (((int) (this.h.measureText(((String) v0)) + v12))));
                            float v5 = this.u ? ((float) (v9 - v10)) : v0_1 + (((float) this.c));
                            float v6_1 = (((float) this.d)) + (((float) v1)) * (this.m + (((float) this.e))) + (this.m - this.l.ascent - this.l.descent) / v13;
                            this.a(arg15, v5, v6_1, ((float) v10), this.n);
                            this.a(arg15, v5, v6_1);
                        }
                    }
                } else if (this.t) {
                    arg15.drawText(this.f, 0f, (((float) this.d)) - this.k.ascent, this.h);
                    v3_1 = (((float) this.d)) + this.m + (((float) this.e)) - this.l.ascent;
                    this.a(arg15, 0f, v3_1, ((float) v10), this.n);
                    this.a(arg15, 0f, v3_1);
                } else {
                    arg15.drawText(this.f, 0f, ((((float) v1)) - this.k.ascent - this.k.descent) / v13, this.h);
                    float v2 = this.u ? ((float) (v9 - v10)) : ((float) (this.c + v3));
                    v3_1 = ((((float) v1)) - this.l.ascent - this.l.descent) / v13;
                    this.a(arg15, v2, v3_1, ((float) v10), this.n);
                    this.a(arg15, v2, v3_1);
                }
            }
        }
    }

    protected void onMeasure(int arg7, int arg8) {
        int v0;
        float v4 = 0.999f;
        if (!TextUtils.isEmpty(this.f)) {
            v0 = this.getLineCount();
            v0 = !this.t || !this.s ? ((int) ((((float) ((v0 - 1) * this.e))) + ((((float) (this.d * 2))) + this.m * (((float) v0))) + v4)) : ((int) ((((float) (v0 * this.e))) + ((((float) (this.d * 2))) + this.m * (((float) v0))) + this.n + v4));
            arg8 = View.MeasureSpec.makeMeasureSpec(v0, MeasureSpec.EXACTLY);
        }

        v0 = View.MeasureSpec.getSize(arg7);
        if (v0 > this.getScreenWidth() && this.o > 0) {
            v0 = this.o;
        }

        this.o = v0;
        super.onMeasure(View.MeasureSpec.makeMeasureSpec(v0, MeasureSpec.EXACTLY), arg8);
    }

    public boolean onTouchEvent(MotionEvent arg6) {
        int v2 = arg6.getActionMasked();
        int v0 = ((int) arg6.getX());
        int v3 = ((int) arg6.getY());
        boolean v0_1 = this.x != null ? this.x.contains(v0, v3) : false;
        switch (v2) {
            case 1:
            case 3: {
                v0_1 = false;
                break;
            }
        }

        if (this.v != v0_1) {
            this.v = v0_1;
            this.invalidate();
        }

        return false;
    }

    public void setAlignRight(boolean arg1) {
        this.u = arg1;
    }

    public void setContentTextColor(int arg2) {
        this.h.setColor(arg2);
    }

    public void setContentTextSize(float arg3) {
        this.h.setTextSize(((float) (((int) TypedValue.applyDimension(1, arg3, this.b)))));
        this.k = this.h.getFontMetrics();
        this.m = this.k.descent - this.k.ascent;
    }

    public void setContentTypeface(Typeface arg2) {
        this.h.setTypeface(arg2);
    }

    public void setLineSpacing(int arg1) {
        this.e = arg1;
    }

    public void setMaxLines(int arg2) {
        if (arg2 > 0) {
            this.y = arg2;
            this.c = 0;
            this.invalidate();
            this.requestLayout();
        }
    }

    public void setMultiLineVerticalPadding(int arg1) {
        this.d = arg1;
    }

    public void setPadding(int arg1) {
        this.c = arg1;
    }

    public void setSuffixPressedBgColor(int arg4) {
        try {
            int v0_1 = this.a.getColor(arg4);
            if (this.j == null) {
                this.j = new Paint();
                this.j.setAntiAlias(true);
            }

            this.j.setColor(v0_1);
        } catch (Exception v0) {
            v0.printStackTrace();
        }
    }

    public void setSuffixTextColor(int arg2) {
        this.i.setColor(arg2);
    }

    public void setSuffixTextSize(float arg3) {
        this.i.setTextSize(((float) (((int) TypedValue.applyDimension(1, arg3, this.b)))));
        this.l = this.i.getFontMetrics();
        this.n = this.l.descent - this.l.ascent;
    }

    public void setText(String arg2) {
        this.a(arg2, "全文");
    }
}

